package com.sskj.givecoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.givecoin.R;
import com.sskj.givecoin.adapter.MyFragmentPagerAdapter;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.m.bean.TabEntity;
import com.sskj.givecoin.p.activity.FabiSellListPresenter;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.v.base.BaseActivity;
import com.sskj.givecoin.v.fragment.FabiBuyAndSellDetailFragment;
import com.sskj.givecoin.v.fragment.FabuRecordFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FabiSellListActivity extends BaseActivity<FabiSellListPresenter> {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private String[] mTitles = {App.INSTANCE.getString(R.string.fabi_record_sell), App.INSTANCE.getString(R.string.fabi_record_buy)};
    private ArrayList<Fragment> mFragments;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fabi_sell_list;
    }

    @Override
    public FabiSellListPresenter getPresenter() {
        return new FabiSellListPresenter();
    }

    @Override
    protected void initData() {
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);

        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        mFragments = new ArrayList<>();
        mFragments.add(FabuRecordFragment.newInstance(false));  // 出售列表
        mFragments.add(FabuRecordFragment.newInstance(true));   // 求购列表
        ClickUtil.click(ivBack, () -> {
            finish();
        });

    }

    public static void start(Context context) {
        Intent starter = new Intent(context, FabiSellListActivity.class);
        context.startActivity(starter);
    }

}
